home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Development Kits / Mac OS / USB DDK 1.4.6f4 / Examples / USBKeypad / USBKeypadKeyIn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-25  |  24.4 KB  |  991 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        USBKeypadKeyIn.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999-2000 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12.  
  13. /*
  14.  USB Keyboard Translation to Macintosh 
  15.  */
  16.  
  17. #include <Types.h>
  18. #include <Events.h>
  19. #include <Resources.h>
  20. #include <LowMem.h>
  21. #include <USB.h>
  22. #include <Timer.h>
  23. #include "USBKeypad.h"
  24.  
  25. extern    usbKeyPadPBStruct myParamBlock;
  26. extern    usbKeyPadPBStruct shimParamBlock;
  27.  
  28.  
  29. #define DOWN            0
  30. #define UP                1
  31.  
  32. #define TRUE            1
  33. #define FALSE            0
  34.  
  35. #define FakeADBAddr        16
  36. #define FakeKBDType        2        // this should be the same as the Apple extended keyboard for now
  37.  
  38. #define kRawEscapeKey    0x35
  39.  
  40. typedef struct {
  41.     TMTask         theTask;
  42.     EventKind     eventNum;
  43.     UInt32         eventMsg;
  44. } TMTaskData;
  45.  
  46. static UInt32    myKeyMAP[4];
  47. static UInt32    keyTransState;
  48. static Handle    handleKCHR;
  49. static UInt8*    KCHRptr;
  50. static TMTaskData gTMTaskData;
  51.  
  52. typedef KeyMap * KeyMapPtr;
  53.  
  54. /* prototypes */
  55. Boolean KeyInArray(UInt8 key, UInt8 *array, UInt16 len);
  56. Boolean SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value); 
  57. void  PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state);
  58. pascal OSErr MyPostEvent(EventKind eventNum, UInt32 eventMsg);
  59. void TimerCompletion(TMTaskData* taskData);
  60.  
  61. /* when we move to master interfaces we can get this stuff from LowMemPriv.h */
  62. /* be sure to turn on DIRECT_LOWMEM_ACCESSORS */
  63. #define LMSetKbdVars(value) ((*(short *)0x0216) = (value))
  64. #define LMSetKeyLast(value) ((*(short *)0x0184) = (value))
  65. #define LMSetKeyTime(value) ((*(long *)0x0186) = (value))
  66. #define LMSetKeyRepTime(value) ((*(long *)0x018A) = (value))
  67. #define LMSetKeyMap(KeyMapValue)    BlockMoveData((Ptr)(KeyMapValue), (Ptr)0x0174, sizeof(KeyMap))
  68.  
  69. // index represents USB keyboard usage value, content is Mac virtual keycode
  70. static UInt8    USBKMAPNumLockDown[256] = {  
  71.     0xFF,     /* 00 no event */        
  72.     0xFF,    /* 01 ErrorRollOver */    
  73.     0xFF,    /* 02 POSTFail */    
  74.     0xFF,    /* 03 ErrorUndefined */    
  75.     0x00,    /* 04 A */
  76.     0x0B,    /* 05 B */
  77.     0x08,    /* 06 C */
  78.     0x02,    /* 07 D */
  79.     0x0E,    /* 08 E */
  80.     0x03,    /* 09 F */
  81.     0x05,    /* 0A G */
  82.     0x04,    /* 0B H */
  83.     0x22,    /* 0C I */
  84.     0x26,    /* 0D J */
  85.     0x28,    /* 0E K */
  86.     0x25,    /* 0F L */
  87.  
  88.     0x2E,     /* 10 M */        
  89.     0x2D,    /* 11 N */    
  90.     0x1F,    /* 12 O */    
  91.     0x23,    /* 13 P */    
  92.     0x0C,    /* 14 Q */
  93.     0x0F,    /* 15 R */
  94.     0x01,    /* 16 S */
  95.     0x11,    /* 17 T */
  96.     0x20,    /* 18 U */
  97.     0x09,    /* 19 V */
  98.     0x0D,    /* 1A W */
  99.     0x07,    /* 1B X */
  100.     0x10,    /* 1C Y */
  101.     0x06,    /* 1D Z */
  102.     0x12,    /* 1E 1/! */
  103.     0x13,    /* 1F 2/@ */
  104.  
  105.     0x14,     /* 20 3 # */        
  106.     0x15,    /* 21 4 $ */    
  107.     0x17,    /* 22 5 % */    
  108.     0x16,    /* 23 6 ^ */    
  109.     0x1A,    /* 24 7 & */
  110.     0x1C,    /* 25 8 * */
  111.     0x19,    /* 26 9 ( */
  112.     0x1D,    /* 27 0 ) */
  113.     0x24,    /* 28 Return (Enter) */
  114.     0x35,    /* 29 ESC */
  115.     0x33,    /* 2A Delete (Backspace) */
  116.     0x30,    /* 2B Tab */
  117.     0x31,    /* 2C Spacebar */
  118.     0x1B,    /* 2D - _ */
  119.     0x18,    /* 2E = + */
  120.     0x21,    /* 2F [ { */
  121.  
  122.     0x1E,     /* 30 ] } */        
  123.     0x2A,    /* 31 \ | */    
  124.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  125.     0x29,    /* 33 ; : */    
  126.     0x27,    /* 34 ' " */
  127.     0x32,    /* 35 ` ~ */
  128.     0x2B,    /* 36 , < */
  129.     0x2F,    /* 37 . > */
  130.     0x2C,    /* 38 / ? */
  131.     0x39,    /* 39 Caps Lock */
  132.     0x7A,    /* 3A F1 */
  133.     0x78,    /* 3B F2 */
  134.     0x63,    /* 3C F3 */
  135.     0x76,    /* 3D F4 */
  136.     0x60,    /* 3E F5 */
  137.     0x61,    /* 3F F6 */
  138.  
  139.     0x62,     /* 40 F7 */        
  140.     0x64,    /* 41 F8 */    
  141.     0x65,    /* 42 F9 */    
  142.     0x6D,    /* 43 F10 */    
  143.     0x67,    /* 44 F11 */
  144.     0x6F,    /* 45 F12 */
  145.     0x69,    /* 46 F13/PrintScreen */
  146.     0x6B,    /* 47 F14/ScrollLock */
  147.     0x71,    /* 48 F15/Pause */                
  148.     0x72,    /* 49 Insert */
  149.     0x73,    /* 4A Home */
  150.     0x74,    /* 4B PageUp */
  151.     0x75,    /* 4C Delete Forward */
  152.     0x77,    /* 4D End */
  153.     0x79,    /* 4E PageDown */
  154.     0x7C,    /* 4F RightArrow */
  155.  
  156.     0x7B,     /* 50 LeftArrow */        
  157.     0x7D,    /* 51 DownArrow */    
  158.     0x7E,    /* 52 UpArrow */    
  159.     0x47,    /* 53 NumLock/Clear */    
  160.     0x4B,    /* 54 Keypad / */
  161.     0x43,    /* 55 Keypad * */
  162.     0x4E,    /* 56 Keypad - */
  163.     0x45,    /* 57 Keypad + */
  164.     0x4C,    /* 58 Keypad Enter */
  165.     0x53,    /* 59 Keypad 1 */
  166.     0x54,    /* 5A Keypad 2 */
  167.     0x55,    /* 5B Keypad 3 */
  168.     0x56,    /* 5C Keypad 4 */
  169.     0x57,    /* 5D Keypad 5 */
  170.     0x58,    /* 5E Keypad 6 */
  171.     0x59,    /* 5F Keypad 7 */
  172.  
  173.     0x5B,     /* 60 Keypad 8 */        
  174.     0x5C,    /* 61 Keypad 9 */    
  175.     0x52,    /* 62 Keypad 0 */    
  176.     0x41,    /* 63 Keypad . */    
  177.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  178.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  179.     0x7F,    /* 66 PowerKey  */
  180.     0x51,    /* 67 Keypad = */
  181.     0x69,    /* 68 F13 */
  182.     0x6B,    /* 69 F14 */
  183.     0x71,    /* 6A F15 */
  184.     0xFF,    /* 6B F16 */
  185.     0xFF,    /* 6C F17 */
  186.     0xFF,    /* 6D F18 */
  187.     0xFF,    /* 6E F19 */
  188.     0xFF,    /* 6F F20 */
  189.  
  190.     0x5B,     /* 70 F21 */        
  191.     0x5C,    /* 71 F22 */    
  192.     0x52,    /* 72 F23 */    
  193.     0x41,    /* 73 F24 */    
  194.     0xFF,    /* 74 Execute */
  195.     0xFF,    /* 75 Help */
  196.     0x7F,    /* 76 Menu */
  197.     0x4C,    /* 77 Select */
  198.     0x69,    /* 78 Stop */
  199.     0x6B,    /* 79 Again */
  200.     0x71,    /* 7A Undo */
  201.     0xFF,    /* 7B Cut */
  202.     0xFF,    /* 7C Copy */
  203.     0xFF,    /* 7D Paste */
  204.     0xFF,    /* 7E Find */
  205.     0xFF,    /* 7F Mute */
  206.     
  207.     0xFF,     /* 80 no event */        
  208.     0xFF,    /* 81 no event */    
  209.     0xFF,    /* 82 no event */    
  210.     0xFF,    /* 83 no event */    
  211.     0xFF,    /* 84 no event */
  212.     0xFF,    /* 85 no event */
  213.     0xFF,    /* 86 no event */
  214.     0xFF,    /* 87 no event */
  215.     0xFF,    /* 88 no event */
  216.     0xFF,    /* 89 no event */
  217.     0xFF,    /* 8A no event */
  218.     0xFF,    /* 8B no event */
  219.     0xFF,    /* 8C no event */
  220.     0xFF,    /* 8D no event */
  221.     0xFF,    /* 8E no event */
  222.     0xFF,    /* 8F no event */
  223.  
  224.     0xFF,     /* 90 no event */        
  225.     0xFF,    /* 91 no event */    
  226.     0xFF,    /* 92 no event */    
  227.     0xFF,    /* 93 no event */    
  228.     0xFF,    /* 94 no event */
  229.     0xFF,    /* 95 no event */
  230.     0xFF,    /* 96 no event */
  231.     0xFF,    /* 97 no event */
  232.     0xFF,    /* 98 no event */
  233.     0xFF,    /* 99 no event */
  234.     0xFF,    /* 9A no event */
  235.     0xFF,    /* 9B no event */
  236.     0xFF,    /* 9C no event */
  237.     0xFF,    /* 9D no event */
  238.     0xFF,    /* 9E no event */
  239.     0xFF,    /* 9F no event */
  240.  
  241.     0xFF,     /* A0 no event */        
  242.     0xFF,    /* A1 no event */    
  243.     0xFF,    /* A2 no event */    
  244.     0xFF,    /* A3 no event */    
  245.     0xFF,    /* A4 no event */
  246.     0xFF,    /* A5 no event */
  247.     0xFF,    /* A6 no event */
  248.     0xFF,    /* A7 no event */
  249.     0xFF,    /* A8 no event */
  250.     0xFF,    /* A9 no event */
  251.     0xFF,    /* AA no event */
  252.     0xFF,    /* AB no event */
  253.     0xFF,    /* AC no event */
  254.     0xFF,    /* AD no event */
  255.     0xFF,    /* AE no event */
  256.     0xFF,    /* AF no event */
  257.  
  258.     0xFF,     /* B0 no event */        
  259.     0xFF,    /* B1 no event */    
  260.     0xFF,    /* B2 no event */    
  261.     0xFF,    /* B3 no event */    
  262.     0xFF,    /* B4 no event */
  263.     0xFF,    /* B5 no event */
  264.     0xFF,    /* B6 no event */
  265.     0xFF,    /* B7 no event */
  266.     0xFF,    /* B8 no event */
  267.     0xFF,    /* B9 no event */
  268.     0xFF,    /* BA no event */
  269.     0xFF,    /* BB no event */
  270.     0xFF,    /* BC no event */
  271.     0xFF,    /* BD no event */
  272.     0xFF,    /* BE no event */
  273.     0xFF,    /* BF no event */
  274.  
  275.     0xFF,     /* C0 no event */        
  276.     0xFF,    /* C1 no event */    
  277.     0xFF,    /* C2 no event */    
  278.     0xFF,    /* C3 no event */    
  279.     0xFF,    /* C4 no event */
  280.     0xFF,    /* C5 no event */
  281.     0xFF,    /* C6 no event */
  282.     0xFF,    /* C7 no event */
  283.     0xFF,    /* C8 no event */
  284.     0xFF,    /* C9 no event */
  285.     0xFF,    /* CA no event */
  286.     0xFF,    /* CB no event */
  287.     0xFF,    /* CC no event */
  288.     0xFF,    /* CD no event */
  289.     0xFF,    /* CE no event */
  290.     0xFF,    /* CF no event */
  291.  
  292.     0xFF,     /* D0 no event */        
  293.     0xFF,    /* D1 no event */    
  294.     0xFF,    /* D2 no event */    
  295.     0xFF,    /* D3 no event */    
  296.     0xFF,    /* D4 no event */
  297.     0xFF,    /* D5 no event */
  298.     0xFF,    /* D6 no event */
  299.     0xFF,    /* D7 no event */
  300.     0xFF,    /* D8 no event */
  301.     0xFF,    /* D9 no event */
  302.     0xFF,    /* DA no event */
  303.     0xFF,    /* DB no event */
  304.     0xFF,    /* DC no event */
  305.     0xFF,    /* DD no event */
  306.     0xFF,    /* DE no event */
  307.     0xFF,    /* DF no event */
  308.  
  309.     0x3B,     /* E0 left control key */        
  310.     0x38,    /* E1 left shift key key */    
  311.     0x3A,    /* E2 left alt/option key */    
  312.     0x37,    /* E3 left GUI (windows/cmd) key */    
  313.     
  314.     0x3E,    /* E4 right control key */
  315.     0x3C,    /* E5 right shift key key */
  316.     0x3D,    /* E6 right alt/option key */
  317.     0x37,    /* E7 right GUI (windows/cmd) key */
  318.     0xFF,    /* E8 no event */
  319.     0xFF,    /* E9 no event */
  320.     0xFF,    /* EA no event */
  321.     0xFF,    /* EB no event */
  322.     0xFF,    /* EC no event */
  323.     0xFF,    /* ED no event */
  324.     0xFF,    /* EE no event */
  325.     0xFF,    /* EF no event */
  326.     
  327.     0xFF,     /* F0 no event */        
  328.     0xFF,    /* F1 no event */    
  329.     0xFF,    /* F2 no event */    
  330.     0xFF,    /* F3 no event */    
  331.     0xFF,    /* F4 no event */
  332.     0xFF,    /* F5 no event */
  333.     0xFF,    /* F6 no event */
  334.     0xFF,    /* F7 no event */
  335.     0xFF,    /* F8 no event */
  336.     0xFF,    /* F9 no event */
  337.     0xFF,    /* FA no event */
  338.     0xFF,    /* FB no event */
  339.     0xFF,    /* FC no event */
  340.     0xFF,    /* FD no event */
  341.     0xFF,    /* FE no event */
  342.     0xFF,    /* FF no event */
  343. };
  344.         
  345. // index represents USB keyboard usage value, content is Mac virtual keycode
  346. static UInt8    USBKMAPNumLockUp[256] = {  
  347.     0xFF,     /* 00 no event */        
  348.     0xFF,    /* 01 ErrorRollOver */    
  349.     0xFF,    /* 02 POSTFail */    
  350.     0xFF,    /* 03 ErrorUndefined */    
  351.     0x00,    /* 04 A */
  352.     0x0B,    /* 05 B */
  353.     0x08,    /* 06 C */
  354.     0x02,    /* 07 D */
  355.     0x0E,    /* 08 E */
  356.     0x03,    /* 09 F */
  357.     0x05,    /* 0A G */
  358.     0x04,    /* 0B H */
  359.     0x22,    /* 0C I */
  360.     0x26,    /* 0D J */
  361.     0x28,    /* 0E K */
  362.     0x25,    /* 0F L */
  363.  
  364.     0x2E,     /* 10 M */        
  365.     0x2D,    /* 11 N */    
  366.     0x1F,    /* 12 O */    
  367.     0x23,    /* 13 P */    
  368.     0x0C,    /* 14 Q */
  369.     0x0F,    /* 15 R */
  370.     0x01,    /* 16 S */
  371.     0x11,    /* 17 T */
  372.     0x20,    /* 18 U */
  373.     0x09,    /* 19 V */
  374.     0x0D,    /* 1A W */
  375.     0x07,    /* 1B X */
  376.     0x10,    /* 1C Y */
  377.     0x06,    /* 1D Z */
  378.     0x12,    /* 1E 1/! */
  379.     0x13,    /* 1F 2/@ */
  380.  
  381.     0x14,     /* 20 3 # */        
  382.     0x15,    /* 21 4 $ */    
  383.     0x17,    /* 22 5 % */    
  384.     0x16,    /* 23 6 ^ */    
  385.     0x1A,    /* 24 7 & */
  386.     0x1C,    /* 25 8 * */
  387.     0x19,    /* 26 9 ( */
  388.     0x1D,    /* 27 0 ) */
  389.     0x24,    /* 28 Return (Enter) */
  390.     0x35,    /* 29 ESC */
  391.     0x33,    /* 2A Delete (Backspace) */
  392.     0x30,    /* 2B Tab */
  393.     0x31,    /* 2C Spacebar */
  394.     0x1B,    /* 2D - _ */
  395.     0x18,    /* 2E = + */
  396.     0x21,    /* 2F [ { */
  397.  
  398.     0x1E,     /* 30 ] } */        
  399.     0x2A,    /* 31 \ | */    
  400.     0xFF,    /* 32 Non-US # and ~ (what?!!!) */    
  401.     0x29,    /* 33 ; : */    
  402.     0x27,    /* 34 ' " */
  403.     0x32,    /* 35 ` ~ */
  404.     0x2B,    /* 36 , < */
  405.     0x2F,    /* 37 . > */
  406.     0x2C,    /* 38 / ? */
  407.     0x39,    /* 39 Caps Lock */
  408.     0x7A,    /* 3A F1 */
  409.     0x78,    /* 3B F2 */
  410.     0x63,    /* 3C F3 */
  411.     0x76,    /* 3D F4 */
  412.     0x60,    /* 3E F5 */
  413.     0x61,    /* 3F F6 */
  414.  
  415.     0x62,     /* 40 F7 */        
  416.     0x64,    /* 41 F8 */    
  417.     0x65,    /* 42 F9 */    
  418.     0x6D,    /* 43 F10 */    
  419.     0x67,    /* 44 F11 */
  420.     0x6F,    /* 45 F12 */
  421.     0x69,    /* 46 F13/PrintScreen */
  422.     0x6B,    /* 47 F14/ScrollLock */
  423.     0x71,    /* 48 F15/Pause */                
  424.     0x72,    /* 49 Insert */
  425.     0x73,    /* 4A Home */
  426.     0x74,    /* 4B PageUp */
  427.     0x75,    /* 4C Delete Forward */
  428.     0x77,    /* 4D End */
  429.     0x79,    /* 4E PageDown */
  430.     0x7C,    /* 4F RightArrow */
  431.  
  432.     0x7B,     /* 50 LeftArrow */        
  433.     0x7D,    /* 51 DownArrow */    
  434.     0x7E,    /* 52 UpArrow */    
  435.     0x47,    /* 53 NumLock/Clear */    
  436.     0x4B,    /* 54 Keypad / */
  437.     0x43,    /* 55 Keypad * */
  438.     0x4E,    /* 56 Keypad - */
  439.     0x45,    /* 57 Keypad + */
  440.     0x4C,    /* 58 Keypad Enter */
  441.     0x77,    /* 59 Keypad 1 = End on keyapd*/
  442.     0x7D,    /* 5A Keypad 2 = Down Arrow on keypad*/
  443.     0x79,    /* 5B Keypad 3 = Page Down on keypad*/
  444.     0x7B,    /* 5C Keypad 4 = Left Arrow on keypad*/
  445.     0x57,    /* 5D Keypad 5 */
  446.     0x7C,    /* 5E Keypad 6 = Right Arrow on keypad*/
  447.     0x73,    /* 5F Keypad 7 = HOME on keypad*/
  448.  
  449.     0x7E,     /* 60 Keypad 8 = Up Arrow on keypad*/        
  450.     0x74,    /* 61 Keypad 9 = Page Up on keypad*/    
  451.     0x72,    /* 62 Keypad 0 = Insert on keypad*/    
  452.     0x75,    /* 63 Keypad . = Delete (forward) on keypad*/    
  453.     0xFF,    /* 64 Non-US \ and  | (what ??!!) */
  454.     0x6E,    /* 65 ApplicationKey (not on a mac!)*/
  455.     0x7F,    /* 66 PowerKey  */
  456.     0x51,    /* 67 Keypad = */
  457.     0x69,    /* 68 F13 */
  458.     0x6B,    /* 69 F14 */
  459.     0x71,    /* 6A F15 */
  460.     0xFF,    /* 6B F16 */
  461.     0xFF,    /* 6C F17 */
  462.     0xFF,    /* 6D F18 */
  463.     0xFF,    /* 6E F19 */
  464.     0xFF,    /* 6F F20 */
  465.  
  466.     0x5B,     /* 70 F21 */        
  467.     0x5C,    /* 71 F22 */    
  468.     0x52,    /* 72 F23 */    
  469.     0x41,    /* 73 F24 */    
  470.     0xFF,    /* 74 Execute */
  471.     0xFF,    /* 75 Help */
  472.     0x7F,    /* 76 Menu */
  473.     0x4C,    /* 77 Select */
  474.     0x69,    /* 78 Stop */
  475.     0x6B,    /* 79 Again */
  476.     0x71,    /* 7A Undo */
  477.     0xFF,    /* 7B Cut */
  478.     0xFF,    /* 7C Copy */
  479.     0xFF,    /* 7D Paste */
  480.     0xFF,    /* 7E Find */
  481.     0xFF,    /* 7F Mute */
  482.     
  483.     0xFF,     /* 80 no event */        
  484.     0xFF,    /* 81 no event */    
  485.     0xFF,    /* 82 no event */    
  486.     0xFF,    /* 83 no event */    
  487.     0xFF,    /* 84 no event */
  488.     0xFF,    /* 85 no event */
  489.     0xFF,    /* 86 no event */
  490.     0xFF,    /* 87 no event */
  491.     0xFF,    /* 88 no event */
  492.     0xFF,    /* 89 no event */
  493.     0xFF,    /* 8A no event */
  494.     0xFF,    /* 8B no event */
  495.     0xFF,    /* 8C no event */
  496.     0xFF,    /* 8D no event */
  497.     0xFF,    /* 8E no event */
  498.     0xFF,    /* 8F no event */
  499.  
  500.     0xFF,     /* 90 no event */        
  501.     0xFF,    /* 91 no event */    
  502.     0xFF,    /* 92 no event */    
  503.     0xFF,    /* 93 no event */    
  504.     0xFF,    /* 94 no event */
  505.     0xFF,    /* 95 no event */
  506.     0xFF,    /* 96 no event */
  507.     0xFF,    /* 97 no event */
  508.     0xFF,    /* 98 no event */
  509.     0xFF,    /* 99 no event */
  510.     0xFF,    /* 9A no event */
  511.     0xFF,    /* 9B no event */
  512.     0xFF,    /* 9C no event */
  513.     0xFF,    /* 9D no event */
  514.     0xFF,    /* 9E no event */
  515.     0xFF,    /* 9F no event */
  516.  
  517.     0xFF,     /* A0 no event */        
  518.     0xFF,    /* A1 no event */    
  519.     0xFF,    /* A2 no event */    
  520.     0xFF,    /* A3 no event */    
  521.     0xFF,    /* A4 no event */
  522.     0xFF,    /* A5 no event */
  523.     0xFF,    /* A6 no event */
  524.     0xFF,    /* A7 no event */
  525.     0xFF,    /* A8 no event */
  526.     0xFF,    /* A9 no event */
  527.     0xFF,    /* AA no event */
  528.     0xFF,    /* AB no event */
  529.     0xFF,    /* AC no event */
  530.     0xFF,    /* AD no event */
  531.     0xFF,    /* AE no event */
  532.     0xFF,    /* AF no event */
  533.  
  534.     0xFF,     /* B0 no event */        
  535.     0xFF,    /* B1 no event */    
  536.     0xFF,    /* B2 no event */    
  537.     0xFF,    /* B3 no event */    
  538.     0xFF,    /* B4 no event */
  539.     0xFF,    /* B5 no event */
  540.     0xFF,    /* B6 no event */
  541.     0xFF,    /* B7 no event */
  542.     0xFF,    /* B8 no event */
  543.     0xFF,    /* B9 no event */
  544.     0xFF,    /* BA no event */
  545.     0xFF,    /* BB no event */
  546.     0xFF,    /* BC no event */
  547.     0xFF,    /* BD no event */
  548.     0xFF,    /* BE no event */
  549.     0xFF,    /* BF no event */
  550.  
  551.     0xFF,     /* C0 no event */        
  552.     0xFF,    /* C1 no event */    
  553.     0xFF,    /* C2 no event */    
  554.     0xFF,    /* C3 no event */    
  555.     0xFF,    /* C4 no event */
  556.     0xFF,    /* C5 no event */
  557.     0xFF,    /* C6 no event */
  558.     0xFF,    /* C7 no event */
  559.     0xFF,    /* C8 no event */
  560.     0xFF,    /* C9 no event */
  561.     0xFF,    /* CA no event */
  562.     0xFF,    /* CB no event */
  563.     0xFF,    /* CC no event */
  564.     0xFF,    /* CD no event */
  565.     0xFF,    /* CE no event */
  566.     0xFF,    /* CF no event */
  567.  
  568.     0xFF,     /* D0 no event */        
  569.     0xFF,    /* D1 no event */    
  570.     0xFF,    /* D2 no event */    
  571.     0xFF,    /* D3 no event */    
  572.     0xFF,    /* D4 no event */
  573.     0xFF,    /* D5 no event */
  574.     0xFF,    /* D6 no event */
  575.     0xFF,    /* D7 no event */
  576.     0xFF,    /* D8 no event */
  577.     0xFF,    /* D9 no event */
  578.     0xFF,    /* DA no event */
  579.     0xFF,    /* DB no event */
  580.     0xFF,    /* DC no event */
  581.     0xFF,    /* DD no event */
  582.     0xFF,    /* DE no event */
  583.     0xFF,    /* DF no event */
  584.  
  585.     0x3B,     /* E0 left control key */        
  586.     0x38,    /* E1 left shift key key */    
  587.     0x3A,    /* E2 left alt/option key */    
  588.     0x37,    /* E3 left GUI (windows/cmd) key */    
  589.     
  590.     0x3E,    /* E4 right control key */
  591.     0x3C,    /* E5 right shift key key */
  592.     0x3D,    /* E6 right alt/option key */
  593.     0x37,    /* E7 right GUI (windows/cmd) key */
  594.     0xFF,    /* E8 no event */
  595.     0xFF,    /* E9 no event */
  596.     0xFF,    /* EA no event */
  597.     0xFF,    /* EB no event */
  598.     0xFF,    /* EC no event */
  599.     0xFF,    /* ED no event */
  600.     0xFF,    /* EE no event */
  601.     0xFF,    /* EF no event */
  602.     
  603.     0xFF,     /* F0 no event */        
  604.     0xFF,    /* F1 no event */    
  605.     0xFF,    /* F2 no event */    
  606.     0xFF,    /* F3 no event */    
  607.     0xFF,    /* F4 no event */
  608.     0xFF,    /* F5 no event */
  609.     0xFF,    /* F6 no event */
  610.     0xFF,    /* F7 no event */
  611.     0xFF,    /* F8 no event */
  612.     0xFF,    /* F9 no event */
  613.     0xFF,    /* FA no event */
  614.     0xFF,    /* FB no event */
  615.     0xFF,    /* FC no event */
  616.     0xFF,    /* FD no event */
  617.     0xFF,    /* FE no event */
  618.     0xFF,    /* FF no event */
  619. };
  620.         
  621. static RoutineDescriptor gTimerCompletionUPP = BUILD_ROUTINE_DESCRIPTOR(uppTimerProcInfo, (ProcPtr)TimerCompletion);
  622.     
  623. void
  624. InitUSBKeyboard()
  625. {
  626.     handleKCHR = GetResource('KCHR',0);    // US keyboard mapping (handled differently by ADB Mgr)
  627.     HLock(handleKCHR);
  628.     KCHRptr = (UInt8 *)*handleKCHR;
  629. }
  630.  
  631.  
  632.  
  633. void PostUSBKeyToMac(UInt16 rawUSBkey)
  634. {
  635. static    UInt8    oldLEDState = 0x00;
  636. static    UInt8    newLEDState = 0x00;
  637.  
  638. static    UInt8    capsLockState = 0x00;
  639. static    UInt8    numLockState = 0x00;
  640. static    UInt8    scrollLockState = 0x00;
  641.  
  642. register UInt8    virtualKeycode, keystate;
  643.  
  644.     
  645.     if (KCHRptr == 0)
  646.     {
  647.         InitUSBKeyboard();
  648.     }
  649.     
  650.     keystate = (rawUSBkey & 0x8000) ? UP : DOWN;
  651.     rawUSBkey &= 0x0FF;
  652.     
  653.     if (keystate == DOWN)
  654.     {
  655.         newLEDState = oldLEDState;
  656.         
  657.         switch (rawUSBkey)
  658.         {
  659. // Note:  This switch statement is being left it to make it easy to add "toggling" keys in the future.
  660. //        it used to support toggled numlock & scroll lock...  It doesn't anymore.
  661.             case kUSBNumLockKey:
  662.                 newLEDState ^= (1 << kNumLockLED);
  663.                 keystate = (newLEDState & (1 << kNumLockLED)) ? DOWN : UP;
  664.                 break;
  665.         }
  666.         
  667.         if (newLEDState != oldLEDState)
  668.         {
  669.             oldLEDState = newLEDState;
  670.             //USBHIDControlDevice(kHIDSetLEDStateByBits, &newLEDState);
  671.         }
  672.     }
  673.     
  674.     if (rawUSBkey == kUSBNumLockKey)
  675.     {
  676.         return;
  677.     }
  678.     
  679.     // look up rawUSBkey in KMAP resource to get virtual keycode
  680.     if (rawUSBkey < sizeof(USBKMAPNumLockDown))
  681.     {
  682.         if (oldLEDState & kNumLockLEDMask)
  683.         {
  684.             virtualKeycode = USBKMAPNumLockDown[rawUSBkey];
  685.         }
  686.         else
  687.         {
  688.             virtualKeycode = USBKMAPNumLockUp[rawUSBkey];
  689.         }
  690.     } 
  691.     else 
  692.     {
  693. //        DebugStr("\pPostUSBKeyToMac: Need bigger KMAP table");
  694.         virtualKeycode = 0xFF;
  695.     }
  696.  
  697.     PostADBKeyToMac(virtualKeycode, keystate);
  698.     if (virtualKeycode == 0x7F)
  699.         PostADBKeyToMac(virtualKeycode, keystate);
  700. }
  701.  
  702. void 
  703. PostADBKeyToMac(UInt16 virtualKeycode, UInt8 state)
  704. {
  705.     UInt32        keyEventMsg;
  706.     EventKind    keyEventKind;
  707.     
  708.     if (virtualKeycode > 127) return;  // not handled by MacOS!
  709.     
  710.     // stop repeating
  711.     LMSetKeyLast(0);
  712.     LMSetKbdVars(0);
  713.     
  714.     // update our keymap
  715.     SetBit((UInt8 *)myKeyMAP, virtualKeycode, state == DOWN ? 1 : 0);
  716.     LMSetKeyMap(&myKeyMAP);
  717.  
  718.     // set this keyboard as the last keyboard
  719.     LMSetKbdLast(FakeADBAddr);
  720.     LMSetKbdType(FakeKBDType);
  721.     
  722.     // call KeyTrans to get character code
  723.     virtualKeycode |= ((myKeyMAP[1]<<9) & 0x00FE00) | ((myKeyMAP[1]>>7) & 0x0100) | ((state==UP) ? 0x080 : 0);
  724.     keyEventMsg = KeyTranslate(KCHRptr, virtualKeycode , &keyTransState);
  725.     virtualKeycode &= 0x7F;
  726.     
  727.     if (gTMTaskData.theTask.qType < 0)    // task is active so kill it
  728.         RmvTime((QElemPtr)&gTMTaskData.theTask);
  729.  
  730.     if (keyEventMsg & 0xFFFF0000) {
  731.         // post event
  732.         UInt32 event = (keyEventMsg & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | ((keyEventMsg>>16) & 0x0FF);
  733.         if (state == DOWN) {
  734.             UInt32 ticks = TickCount();
  735.             LMSetKeyTime(ticks);
  736.             LMSetKeyRepTime(ticks);
  737.             LMSetKeyLast(event & 0x0FFFF);
  738.             LMSetKbdVars((event>>16) & 0x0FFFF);
  739.         }        
  740.         
  741.         keyEventKind = (state == DOWN ? keyDown : keyUp);
  742.         
  743.         if (/*!inMacsbug && */ (virtualKeycode == kRawEscapeKey)) {
  744.             gTMTaskData.eventNum = keyEventKind;
  745.             gTMTaskData.eventMsg = event;
  746.             
  747.             gTMTaskData.theTask.tmAddr = &gTimerCompletionUPP;
  748.             gTMTaskData.theTask.tmCount = 0;
  749.             gTMTaskData.theTask.tmWakeUp = 0;
  750.             gTMTaskData.theTask.tmReserved = 0;
  751.             InsTime((QElemPtr)&gTMTaskData.theTask);        // install our task
  752.             PrimeTime((QElemPtr)&gTMTaskData.theTask, 10L);    // run as soon as 68K interrupts are enabled
  753.         }
  754.         else {
  755.             // post the keys directly
  756.             MyPostEvent(keyEventKind, event);
  757.         }
  758.     }
  759.  
  760.     if (keyEventMsg & 0x0000FFFF) {
  761.         // post event
  762.         UInt32 event =((keyEventMsg<<16) & 0xFF000000) | ((FakeADBAddr << 16) & 0x0FF0000) | ((virtualKeycode << 8) & 0x0FF00)  | (keyEventMsg & 0x0FF);
  763.         if (state == DOWN){
  764.             UInt32 ticks = TickCount();
  765.             LMSetKeyTime(ticks);
  766.             LMSetKeyRepTime(ticks);
  767.             LMSetKeyLast(event & 0x0FFFF);
  768.             LMSetKbdVars((event>>16) & 0x0FFFF);
  769.         }        
  770.         
  771.         keyEventKind = (state == DOWN ? keyDown : keyUp);
  772.         
  773.         if (/*!inMacsbug && */ (virtualKeycode == kRawEscapeKey)) {
  774.             gTMTaskData.eventNum = keyEventKind;
  775.             gTMTaskData.eventMsg = event;
  776.             
  777.             gTMTaskData.theTask.tmAddr = &gTimerCompletionUPP;
  778.             gTMTaskData.theTask.tmCount = 0;
  779.             gTMTaskData.theTask.tmWakeUp = 0;
  780.             gTMTaskData.theTask.tmReserved = 0;
  781.             InsTime((QElemPtr)&gTMTaskData.theTask);        // install our task
  782.             PrimeTime((QElemPtr)&gTMTaskData.theTask, 10L);    // run as soon as 68K interrupts are enabled
  783.         }
  784.         else {
  785.             // post the keys directly
  786.             MyPostEvent(keyEventKind, event);
  787.         }
  788.     }
  789. }
  790.  
  791. // Sets the bitmapArray[index] to value
  792. // returns old value;
  793. Boolean
  794. SetBit(UInt8 *bitmapArray, UInt16 index, Boolean value)
  795. {    
  796.     UInt32    mask = 0x1 << (index % 8 );
  797.     Boolean    oldVal;
  798.     
  799.     oldVal = (bitmapArray[index/8] & mask) ? TRUE : FALSE;
  800.     
  801.     if (value){
  802.         bitmapArray[index/8] |= mask;
  803.     }else{
  804.         bitmapArray[index/8] &= ~mask;
  805.     }
  806.     
  807.     return (oldVal);
  808. }
  809.  
  810. void USBDemoKeyIn(UInt32 refcon, void * theData)
  811. {
  812. #pragma unused (refcon)
  813.  
  814. USBHIDDataPtr    pTheKeyboardData;
  815. register        UInt8    i;
  816.  
  817.     pTheKeyboardData = (USBHIDDataPtr)theData;
  818.     for (i=0; i<pTheKeyboardData->kbd.keycount; i++)
  819.     {
  820.         // no shim installed, let's just post some Macintosh keyevents
  821.         PostUSBKeyToMac(pTheKeyboardData->kbd.usbkeycode[i]);
  822.     }
  823.  
  824. }
  825.  
  826. OSStatus USBHIDControlDevice(UInt32 theControlSelector, void * theControlData)
  827. {
  828. #pragma unused (theControlData)
  829.  
  830.     switch (theControlSelector)
  831.     {
  832.         case kHIDSetLEDStateByBits:
  833.             break;
  834.             
  835.         case kHIDEnableDemoMode:
  836.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Enabled", myParamBlock.deviceRef);
  837.             break;
  838.  
  839.         case kHIDDisableDemoMode:
  840.             USBExpertStatus(myParamBlock.deviceRef, "\pTenKey Driver: Demo Mode Disabled", myParamBlock.deviceRef);
  841.             break;
  842.  
  843.         default:
  844.             return paramErr;
  845.     }
  846.     return 0;
  847. }
  848.  
  849. void ProcessInterruptReport(UInt8 hidReport[])
  850. {
  851.  
  852. UInt8    i, j, newkey, oldkey, deltas;
  853.  
  854. UInt8        changedmodifiers, keycount;
  855. USBHIDData    theKeyboardData;
  856. Boolean        keypressedflag, keyreleasedflag;
  857.     
  858.     deltas = 0;
  859.     
  860.     if (myParamBlock.hidEmulationInit == false)
  861.     {
  862.         myParamBlock.hidEmulationInit = true;
  863.         for (i = 0; i < kKeyboardReportSize; i++)
  864.             myParamBlock.oldHIDReport[i] = 0;
  865.     };
  866.     
  867.     myParamBlock.oldHIDReport[1] = 0x0;
  868.     hidReport[1] = 0x0;
  869.     
  870.     for (i = 0; i < kKeyboardReportSize; i++)
  871.     {
  872.         if (hidReport[i] != myParamBlock.oldHIDReport[i])
  873.         {
  874.             deltas++;
  875.         }
  876.     }
  877.     
  878.     if ((myParamBlock.sendRawReportFlag) && deltas)
  879.     {
  880.         USBDemoKeyIn(0xff, (void *)&hidReport[0]);
  881.     }
  882.     else
  883.     {
  884.         if (deltas)
  885.         {
  886.             keycount = 0;
  887.             changedmodifiers = hidReport[0] ^  myParamBlock.oldHIDReport[0];
  888.             
  889.             if (changedmodifiers)
  890.             {
  891.                 for (i = 0; i < kKeyboardModifierBits; i++)
  892.                 {
  893.                     if (changedmodifiers & (1 << i))
  894.                     {
  895.                         if (hidReport[0] & (1 << i))
  896.                         {
  897.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0xe0 + i);
  898.                         }
  899.                         else
  900.                         {
  901.                             theKeyboardData.kbd.usbkeycode[keycount++] = (0x80e0 + i);
  902.                         }
  903.                     }
  904.                 }
  905.                 myParamBlock.oldHIDReport[0] = hidReport[0];
  906.             }
  907.             
  908.             if ((hidReport[kKeyboardOffsetToKeys] == 0) || (hidReport[kKeyboardOffsetToKeys] > 0x03))
  909.             {
  910.                 // While this double loop may look strange, I refer you to appendix C, of the HID Devices specification.
  911.                 // pp. 73 & 74 clearly state that report order is abitrary and does not reflect the order of events.
  912.                 // to quote: "The order of keycodes in array fields has no significance.  Order determination is done
  913.                 // by the host software comparing the contents of the previous report to the current report.  If two or 
  914.                 // more keys are pressed in one report, their order is indeterminate.  Keyboards may buffer events that
  915.                 // would have otherwise resulted in multiple events in a single report".
  916.                 
  917.                 // Because this specification (or lack thereof) states that the order is indeterminant, we have to 
  918.                 // check all the old keys against all the new keys...
  919.                 
  920.                 for (i = kKeyboardOffsetToKeys; i < (kKeyboardOffsetToKeys + kKeyboardReportKeys); i++)
  921.                 {
  922.                     keypressedflag = true;
  923.                     newkey = hidReport[i];
  924.                     
  925.                     keyreleasedflag = true;
  926.                     oldkey = myParamBlock.oldHIDReport[i];                            
  927.                     for (j = kKeyboardOffsetToKeys; j < (kKeyboardOffsetToKeys + kKeyboardReportKeys); j++)                                // then look through all the keys that were previously and are currently reported as pressed
  928.                     {                                    
  929.                         if (newkey == myParamBlock.oldHIDReport[j])        // was this new key already pressed?
  930.                         {                                
  931.                             keypressedflag = false;                        // if it was, then don't report it as being pressed.
  932.                         }
  933.                         
  934.                         if (oldkey == hidReport[j])                        // Is the old key still pressed?
  935.                         {                                
  936.                             keyreleasedflag = false;                    // If yes, then don't report it as being released
  937.                         }
  938.                     }
  939.                     if ((newkey > 0x03) && keypressedflag)
  940.                     {
  941.                         theKeyboardData.kbd.usbkeycode[keycount++] = newkey;
  942.                     }
  943.                     
  944.                     if ((oldkey > 0x03) && keyreleasedflag)
  945.                     {
  946.                         theKeyboardData.kbd.usbkeycode[keycount++] = (oldkey + 0x8000);
  947.                     }
  948.                     
  949.                     if (keycount > 20)                                    // a worse case scenario is:
  950.                     {                                                    // 4 modifier keys up
  951.                         break;                                            // the other 4 modifier keys down
  952.                     }                                                    // 6 previously reported keys up
  953.                 }                                                        // 6 newly reported keys down
  954.                                                                         // 6 + 6 + 4 + 4 = 20
  955.                 for (i = 0; i<kKeyboardReportSize; i++)
  956.                 {
  957.                     myParamBlock.oldHIDReport[i] = hidReport[i];
  958.                 };
  959.             }
  960.             
  961.             if (keycount)
  962.             {
  963.                 theKeyboardData.kbd.keycount = keycount;
  964.                 USBDemoKeyIn(myParamBlock.interruptRefcon, (void *)&theKeyboardData);
  965.             }
  966.         }
  967.     }
  968. }
  969.  
  970. // This version of PostEvent works around a problem in the force-quit detection where the EventKind
  971. // is being treated (incorrectly) as an UInt32  
  972. pascal OSErr MyPostEvent(EventKind eventNum, UInt32 eventMsg)
  973. {
  974.     return (OSErr) CallUniversalProc(
  975.                  (UniversalProcPtr)GetOSTrapAddress(0xA02F),
  976.                  kRegisterBased |
  977.                  RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
  978.                  REGISTER_ROUTINE_PARAMETER(1, kRegisterA0, SIZE_CODE(sizeof(UInt32))) |
  979.                  REGISTER_ROUTINE_PARAMETER(2, kRegisterD0, SIZE_CODE(sizeof(UInt32))),
  980.                   eventNum, eventMsg); // parameter(s)
  981. }
  982.  
  983. void TimerCompletion(TMTaskData* taskData)
  984. {
  985.     OSErr err;
  986.     
  987.     RmvTime((QElemPtr)&taskData->theTask);
  988.  
  989.     err = MyPostEvent(taskData->eventNum, taskData->eventMsg);
  990. }
  991.